package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.SysEmail;
import com.example.entity.SysSms;
import com.example.entity.SysUser;
import com.example.mapper.SysUserMapper;
import com.example.service.SysEmailService;
import com.example.service.SysSmsService;
import com.example.service.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysEmailService sysEmailService;

    @Resource
    private SysSmsService sysSmsService;

    @Override
    public void saveAndRegister(SysUser sysUser) {
        this.save(sysUser);
        this.sendEmail(sysUser);
        this.sendSms(sysUser);
    }

    @Override
    public void saveAndBatchRegister(List<SysUser> list) {
        this.saveBatch(list);
        List<SysSms> smsList = new ArrayList<>();
        for (SysUser sysUser : list) {
            SysSms sysSms = new SysSms();
            sysSms.setModelId(sysUser.getId());
            sysSms.setModelName(sysUser.getClass().getName());
            sysSms.setContext("恭喜您注册成功");
            smsList.add(sysSms);
        }
        this.sysSmsService.sendBatchSms(smsList);
    }

    @Override
    public void sendEmail(SysUser sysUser) {
        SysEmail sysEmail = new SysEmail();
        sysEmail.setModelId(sysUser.getId());
        sysEmail.setModelName(sysUser.getClass().getName());
        sysEmail.setContext("恭喜您注册成功");
        this.sysEmailService.sendEmail(sysEmail);
    }

    @Override
    public void sendSms(SysUser sysUser) {
        SysSms sysSms = new SysSms();
        sysSms.setModelId(sysUser.getId());
        sysSms.setModelName(sysUser.getClass().getName());
        sysSms.setContext("恭喜您注册成功");
        this.sysSmsService.sendSms(sysSms);
    }
}
